<?php

$grab_image_bin = "/home/luanjunyi/yhhd/py/taras/tools/grab_image";
$grab_text_bin = "/home/luanjunyi/yhhd/py/taras/tools/grab_text";

include_once( 'sina-sdk/config.php' );
include_once( 'sina-sdk/weibooauth.php' );

ob_implicit_flush();

// Check name availability
if (isset($_POST['name'])) {
  $name = $_POST['name'];
  $api = new WeiboClient( WB_AKEY , WB_SKEY , "887be4e8d7a7ae34543f445e2c5f4d0d", "95a80cd7f2bb3616332861c63e0725a8");
  $user = $api->show_user($name);

  if (isset($user['error'])) {
    echo '(' . $name . ') 可以用，恭喜';
  } else {
    echo '(' . $name . ') 已经被别人注册了，别放弃，继续加油哦';
  }

  return;
}

// Check image extraction
if (isset($_GET['url']) && isset($_GET['hint'])) {
    $url = urldecode($_GET['url']);
    $hint = $_GET['hint'];
    /* echo $hint; */
    /* return; */
    chdir('../tmp');
    system('pwd');
    echo '<br/>';
    system('whoami');
    echo '<br/>';
    $cmd = "{$grab_image_bin} '{$url}' '{$hint}' 2>&1";
    echo " $cmd<br/>";
    echo "<pre>";
    system($cmd);
    echo "</pre>";
    return;
}

// Check main text body extraction
if (isset($_GET['text-url'])) {
  $url = $_GET['text-url'];
  chdir('../tmp');
  system('pwd');
  echo '<br/>';
  system('whoami');
  echo '<br/>';
  $cmd = "{$grab_text_bin} '{$url}' 2>&1";
  echo " $cmd<br/>";
  echo "<pre>";
  system($cmd);
  echo "</pre>";
  return;
}

?>

<?php
// load all proxies in DB
require_once '../util.php';
require_once '../config.php';
connect_db();

$sql = "select addr from proxy";
$ret = mysql_query($sql);
if (!$ret) {
  die(mysql_error());
}

$exist_proxy = "";
while ($addr = mysql_fetch_row($ret)) {
  $exist_proxy .= "<span>" . trim($addr[0]). "</span>";
}
$exist_proxy = "<div id='exist-proxy-div'>" . $exist_proxy . "</div>";

?>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>小工具集合</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link rel="stylesheet" type="text/css" href="/lib/css/reset.css">
    <link rel="stylesheet" type="text/css" href="css/misc.css">
    <script type="text/javascript" src="/lib/js/jquery.js"></script>
    <script type="text/javascript" src="/lib/js/sprintf.js"></script>
    <script type="text/javascript" src="js/misc.js"></script>
  </head>

  <body>
    <div id="top-tools">
      <input type="text" name="name" />
      <button id="check">看看名字是否可用~</button>
      <br/>
      <br/>
      <label id="stat"></label>

      <form method="GET" action="inspect-id.php">
      <input type="text" name="email" />
      <input type="submit" id="check-log" value="看看这个ID做了什么" />
      </form>
    </div>

    <div id="grab-div">
      <label for="baseurl">URL:</label>
      <input type="text" name="baseurl" size="50"/>
      <br/>
      <label for="hint">提示:</label>
      <input type="text" name="hint" size="50"/>
      <br/>
      <button id="grab-image">抓图, and I am feeling lucky</button>
      <br/>
      <br/>
      <br/>

      <label for="baseurl">URL:</label>
      <input type="text" name="baseurl-text" size="50"/>
      <br/>
      <button id="grab-text">提取正文</button>

      <div id="proxy-test">
        <form method="get" action="test-proxy.php" target="grab-stat" id="test-one-proxy-form">
          IP地址: <span id="proxy-conflict"> </span><input type="text" name="addr" id="addr-input"/> 
          端口: <input type="text" name="port" />
          用户名: <input type="text" name="user" />
          密码: <input type="text" name="passwd" />
          <input type="submit" value="测试单条代理" />
        </from>
        <br />
        <a href="../proxy-stat/">全部代理状况</a>
        <?= $exist_proxy ?>
      </div>

    </div>



    <iframe id="grab-stat" width="500" height="300">
        
    </iframe>


  </body>

</html>
